package cn.com.do4u.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by chenyu on 15/12/15.
 */
public class LoginFilter implements Filter {

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String ip1 = request.getRemoteAddr();
        RequestDispatcher dispatcher = request.getRequestDispatcher("amezi/login.html");
        HttpSession session = request.getSession();
        String userName = (String) session.getAttribute("userName");
        String ip2 = (String) session.getAttribute("ip");
        if (userName != null && null != ip2 && ip2.equals(ip1)) {
            filterChain.doFilter(servletRequest, servletResponse);

        } else {
            dispatcher.forward(servletRequest, servletResponse);
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {


    }

    public void destroy() {

    }
}
